package spring.chobo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
	private ConnectionMaker connectionMaker;
	String sql,result="";
	
	public UserDao(ConnectionMaker connectionMaker) {
		this.connectionMaker=connectionMaker;
	}

	public void add(User user) throws Exception{
		Connection conn = connectionMaker.makeConnection();
		
		sql="insert into users(id,name, pass) values(?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, user.getId());
		ps.setString(2, user.getName());
		ps.setString(3, user.getPass());
		
		ps.executeUpdate();
		ps.close();
		conn.close();

	}//add
	
	public User get(String id) throws Exception{
		Connection conn = connectionMaker.makeConnection();
		
		sql="select * from users where id =?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, id);
		
		ResultSet rs=ps.executeQuery();
		rs.next();
		
		User user=new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPass(rs.getString("pass"));
		
		rs.close();
		ps.close();
		conn.close();
		
		return user;
	}
	
}//class
